package com.sum.tea.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import java.util.ArrayList;
import java.util.HashMap;
import com.sum.tea.R;

/**
 * 带勾选自定义list适配器
 *
 * @author Administrator
 *
 */
public class MyAdapter_mfil extends BaseAdapter {
    // 填充数据的list
    private ArrayList<String> list;
    //已选中的数据
    private ArrayList<String> slist;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    // 用来导入布局
    private LayoutInflater inflater = null;

    // 构造器
    @SuppressLint("UseSparseArrays")
    public MyAdapter_mfil(ArrayList<String> list,ArrayList<String> slist, Context context) {
        this.list = list;
        this.slist=slist;
        inflater = LayoutInflater.from(context);
        isSelected = new HashMap<Integer, Boolean>();
        // 初始化数据
        initDate();
    }

    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < list.size(); i++) {

            if(slist.contains(list.get(i)))
                getIsSelected().put(i, true);
        else
                getIsSelected().put(i, false);


        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder_mfil holder = null;
        if (convertView == null) {
            holder = new ViewHolder_mfil();
            convertView = inflater.inflate(R.layout.listitemview_mfil, null);
            holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox_fltag);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ViewHolder_mfil) convertView.getTag();
        }
        holder.cb.setText((String) getItem(position));
        // 根据isSelected来设置checkbox的选中状况
        holder.cb.setChecked(getIsSelected().get(position));

        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        MyAdapter_mfil.isSelected = isSelected;
    }

    public static class ViewHolder_mfil {
        public CheckBox cb;
    }
}
